package com.ask.server.controller;

import com.ask.common.result.Result;
import com.ask.pojo.dto.ActivityDTO;
import com.ask.pojo.dto.ActivityQueryDTO;
import com.ask.pojo.entity.Activity;
import com.ask.server.service.IActivityService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/activity")
@Tag(name = "活动接口")
public class ActivityController {
    @Autowired
    private IActivityService activityService;

    /**
     * 新增活动
     *
     * @param activityDTO
     * @return null
     */
    @PostMapping
    @Operation(summary = "新增活动")
    public Result save(@RequestBody ActivityDTO activityDTO) {
        activityService.saveActivity(activityDTO);
        return Result.success();
    }

    /**
     * 编辑活动
     *
     * @param activityDTO
     * @return
     */
    @PutMapping
    @Operation(summary = "编辑活动")
    public Result update(@RequestBody ActivityDTO activityDTO) {
        activityService.updateActivity(activityDTO);
        return Result.success();
    }

    /**
     * 设置活动状态
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/{id}/status")
    @Operation(summary = "设置活动状态")
    public Result setStatus(@PathVariable Long id, @RequestParam Integer status) {
        activityService.setStatus(id, status);
        return Result.success();
    }

    /**
     * 分页查询
     *
     * @param
     * @return
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询")
    public Result pageQuery(@RequestParam(defaultValue = "0") Integer page,
                            @RequestParam(defaultValue = "5") Integer pageSize,
                            @RequestParam(required = false, defaultValue = "") String name) {
        System.out.println("page: " + page);

        Page<Activity> activityPage = activityService.query()
                .like("name", name)
                .page(new Page<>(page, pageSize));
        return Result.success(activityPage.getRecords());
    }

    /**
     * 分页查询(对象传参)
     *
     * @param activityQueryDTO
     * @return
     */
    @PostMapping("/page2") //@GetMapping不支持@RequestBody
    @Operation(summary = "分页查询(对象传参)")
    public Result pageQueryV2(@RequestBody ActivityQueryDTO activityQueryDTO) {
        Page<Activity> activityPage = activityService.query()
                .like("name", activityQueryDTO.getName())
                .eq("category_id", activityQueryDTO.getCategoryId())
                .page(new Page<>(activityQueryDTO.getPage(), activityQueryDTO.getPageSize()));
        return Result.success(activityPage.getRecords());
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @Operation(summary = "批量删除")
    //todo @RequestParam 在这里的作用
    public Result deleteBatch(@RequestParam @NotEmpty(message = "ids cannot be empty") List<Long> ids) {
        activityService.removeBatchByIds(ids);
        return Result.success();
    }

    /**
     * 根据id查询活动
     * @param id
     * @return
     */
    @GetMapping("/activities/{id}") // todo 为什么这里用路径传参？
    @Operation(summary = "根据id查询活动")
    public Result queryById(@PathVariable Long id) {
        Activity activity = activityService.queryById(id);
        if (activity == null)
            return Result.fail("活动不存在！");
        return Result.success(activity);
    }

    /**
     * 根据分类id查询活动
     * @param categoryId
     * @return
     */
    @GetMapping("/category/{id}")
    @Operation(summary = "根据分类id查询活动")
    public Result queryByCategoryId(@PathVariable("id") Long categoryId) {
        List<Activity> activityList = activityService.queryByCategoryId(categoryId);
        if (activityList == null || activityList.isEmpty())
            return Result.fail("不存在该类型的活动！");
        return Result.success(activityList);
    }
}